package com.zxq.ai.application.api.impl;

import com.zxq.ai.application.api.IBUserCashAccount;
import com.zxq.ai.application.exception.CashAccountNotExistException;
import com.zxq.ai.application.repository.CashAccountLogRepository;
import com.zxq.ai.application.repository.CashAccountRepository;
import com.zxq.ai.domain.user.account.CashAccount;
import com.zxq.ai.domain.user.account.CashAccountOperateLog;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * BUser与【现金账户】有关的【用例】
 */
@Service
public class BUserCashAccount implements IBUserCashAccount {

    @Resource
    private CashAccountRepository cashAccountRepository;
    @Resource
    private CashAccountLogRepository cashAccountLogRepository;



    @Override
    public CashAccount queryAccountOverview(String userId) {

        CashAccount cashAccount = cashAccountRepository.queryByAccountId(userId);

        return cashAccount;
    }

    @Override
    public List<CashAccountOperateLog> queryAccountOperateRecord(String userId) {

        List<CashAccountOperateLog> cashAccountOperateLogList = cashAccountLogRepository.queryByAccountId(userId);

        return cashAccountOperateLogList;
    }

    @Override
    public void cashRecharge(String userId, Long amount) {

        CashAccount cashAccount = cashAccountRepository.queryByAccountId(userId);
        if (Objects.isNull(cashAccount)) {
            String errMsg = String.format("用户[%s]的现金账户不存在", userId);
            throw new CashAccountNotExistException(errMsg);
        }

        cashAccount.recharge(amount, userId);
    }

    @Override
    public void cashWithdraw(String userId, Long amount) {
        CashAccount cashAccount = cashAccountRepository.queryByAccountId(userId);
        if (Objects.isNull(cashAccount)) {
            String errMsg = String.format("用户[%s]的现金账户不存在", userId);
            throw new CashAccountNotExistException(errMsg);
        }

        cashAccount.withdraw(amount, userId);
    }
}
